const {DBHOST, DBNAME, DBPORT} = require('../config/config')

module.exports = function (success, error) {
    if (typeof error !== 'function') {
        error = () => {}
    }
    const mongoose = require('mongoose')

    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`)

    mongoose.connection.on('open', () => {
        success()
    })


    mongoose.connection.on('error', () => {
        error()
        console.log('连接失败');
    })

    mongoose.connection.on('close', () => {
        console.log('连接关闭');
    })
}